<template>
	<view class="viewport">
		<!-- 搜索框 -->
		<view class="search">
			<view class="input">
				<text class="icon-search">女靴</text>
			</view>
		</view>
		<!-- 分类 -->
		<view class="categories">
			<!-- 左侧：一级分类 -->
			<scroll-view class="primary" scroll-y>
				<!-- 默认显示全部分类 -->
				<view class="item" :class="{ active: activeIndex === -1 }" @tap="activeIndex = -1">
					<text class="name">全部分类</text>
				</view>
				<!-- 循环一级分类名称 -->
				<view v-for="(item, index) in categories" :key="item.id" class="item"
					:class="{ active: index === activeIndex }" @tap="activeIndex=index">
					<text class="name"> {{item.name}} </text>
				</view>
			</scroll-view>
			<!-- 右侧：二级分类 -->
			<scroll-view class="secondary" scroll-y>
				<view class="panel section">
					<!-- 我想这里的商品图片，文字占同一行，怎么写样式啊 -->
					<navigator v-for="(goods,index) in goodses" :key="goods" class="goods" hover-class="none"
						:url="`/pages/goods/goods?id=${goods.id}`">
						<image class="image"
							src="https://yanxuan-item.nosdn.127.net/674ec7a88de58a026304983dd049ea69.jpg"></image>
						<view class="left">
							<view class="name ellipsis">{{goods.name}}这是一段补加的文字，意思是要显示2行 多余的截断</view>
							<view class="tag">
								<view class="tag-view">
									<uni-tag size="mini" text="自营" type="error" />
								</view>
								<view class="tag-view">
									<uni-tag size="mini" text="7天补价" />
								</view>
							</view>
							<view class="price-icon">
								<view class="price">
									<text class="symbol">¥</text>
									<text class="number">{{goods.price.toFixed(2)}}</text>
								</view>
								<uni-icons v-if="cartNumber === 0 " @tap.stop="onCart(goods,index)" class="icons" type="plus-filled" color="#cf4444"
									size="25"></uni-icons>
									<!-- 执行changeNumber时阻止navigator跳转，用.stop不生效，该怎么写？ -->
								<!-- <uni-section class="icons" type="line" padding> -->
									<uni-number-box v-else  @change.stop="changeNumber($event,index)" :value="cartNumber[index]"/>
								<!-- </uni-section> -->
							</view>
						</view>
					</navigator>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script setup lang="ts">
	import { useCategoryStore } from "@/store/modules/category.ts";
	import { onLoad } from '@dcloudio/uni-app';
	import { computed, reactive, ref, toRefs, watch } from 'vue';
	import { getGoodsBannerApi } from "@/api/banner/index.ts";
	import type { GoodsBannerVo } from '@/api/banner/types.ts';
	import { getGoodsApi } from "@/api/goods/index.ts";
	import type { GoodsVo } from '@/api/goods/types.ts';
	import { getCategoryPanelApi } from "@/api/category/index.ts";
	import type { CategoryVo } from '@/api/category/types.ts';

	const categoryStore = useCategoryStore();
	console.log(categoryStore.categories);
	const activeIndex = ref(0)
	const banners = ref<GoodsBannerVo[]>([]);
	const goodses = ref<GoodsVo[]>([]);
	const cartNumber = ref(0);
	const categories = ref<CategoryVo[]>([]);
	/*轮播图 */
	const getBananer = async () => {
		const res = await getGoodsBannerApi();
		banners.value = res.data
	}
/* 商品分类 */
	
	const getCategory = async () => {
		const res = await getCategoryPanelApi()
		console.log(res)
		categories.value= res.data
	}
	/* 提取二级分类 */
	// const subCagtegories = computed(() => {
	// 	return categoryStore.categories[activeIndex.value]?.children;
	// })
	/* 商品参数 */
	const data = reactive<any>({
		queryParams: {
			pageNum: 1,
			pageSize: 21,
			cate1Id: ''

		}
	});

	const { queryParams } = toRefs(data);
	// 监听activeIndex的变化  
	watch(activeIndex, (newIndex) => {
		const category = categoryStore.categories[newIndex];
		if (category) {
			console.log(category)
			queryParams.value.cate1Id = category.id; // 更新查询参数中的cate1Id  
		} else {
			queryParams.value.cate1Id = ''; // 更新查询参数中的cate1Id  
		}
		getGoods(); // 重新获取商品列表  
	});
	//获取上面被点击的categories的@tap的id值，再将id赋值给queryParams.value.cate1Id
	const getGoods = async () => {
		const res = await getGoodsApi(queryParams.value)
		console.log(res)
		goodses.value = res.rows;
	}
	/* 加入购物车 */
	const onCart = (goods,index) => {
		console.log("加入购物车", e)
		// cartNumber.value[index] = 1;
	}
	const changeNumber = (newValue,index) => {

		// cartNumber.value[index] = newValue
	}

	onLoad(() => {
		// categoryStore.getCategory();
		getBananer();
		getGoods();
		getCategory();
	})
</script>
<style lang="scss" scoped>
	page {
		height: 100%;
		overflow: hidden;
	}

	.viewport {
		height: 100%;
		display: flex;
		flex-direction: column;
	}

	.search {
		padding: 0 30rpx 20rpx;
		background-color: #fff;

		.input {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 64rpx;
			padding-left: 26rpx;
			color: #8b8b8b;
			font-size: 28rpx;
			border-radius: 32rpx;
			background-color: #f3f4f4;
		}
	}

	.icon-search {
		&::before {
			margin-right: 10rpx;
		}
	}

	/* 分类 */
	.categories {
		flex: 1;
		min-height: 400rpx;
		display: flex;
	}

	/* 一级分类 */
	.primary {
		overflow: hidden;
		width: 180rpx;
		flex: none;
		background-color: #f6f6f6;

		.item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 96rpx;
			font-size: 26rpx;
			color: #595c63;
			position: relative;

			&::after {
				content: '';
				position: absolute;
				left: 42rpx;
				bottom: 0;
				width: 96rpx;
				border-top: 1rpx solid #e3e4e7;
			}
		}

		.active {
			background-color: #fff;

			&::before {
				content: '';
				position: absolute;
				left: 0;
				top: 0;
				width: 8rpx;
				height: 100%;
				background-color: #27ba9b;
			}
		}
	}

	.primary .item:last-child::after,
	.primary .active::after {
		display: none;
	}

	/* 二级分类 */
	.secondary {
		background-color: #fff;

		.carousel {
			height: 200rpx;
			margin: 0 30rpx 20rpx;
			border-radius: 4rpx;
			overflow: hidden;
		}

		.panel {
			margin: 0 30rpx 0rpx;
		}

		.title {
			height: 60rpx;
			line-height: 60rpx;
			color: #333;
			font-size: 28rpx;
			border-bottom: 1rpx solid #f7f7f8;

			.more {
				float: right;
				padding-left: 20rpx;
				font-size: 24rpx;
				color: #999;
			}
		}

		.more {
			&::after {
				font-family: 'erabbit' !important;
				content: '\e6c2';
			}
		}

		.section {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			padding: 20rpx 0;
		}

		.goods {
			display: flex;
			align-items: center;
			width: 100%; // 确保商品容器宽度为100%
			height: 200rpx;
			padding: 0 20rpx; // 根据需要调整内边距

			navigator {
				display: flex; // 使 navigator 也使用 flex 布局  
				width: 100%; // 占据全部可用宽度  
				align-items: center; // 保持内容垂直居中  
			}

			image {
				width: 150rpx;
				height: 150rpx;
				border-radius: 10rpx;
				margin-right: 20rpx; // 添加右边距，使图片和文字之间有间隔
			}

			.left {
				display: flex;
				flex-direction: column;
				justify-content: center; // 垂直居中对齐
				flex-grow: 1; // 允许内容增长并填满剩余空间
				overflow: hidden; // 如果内容超出，隐藏多余的部分

				.name {
					width: 300rpx;
					font-size: 26rpx;
					color: #333;
					// white-space: nowrap; // 强制名称在一行显示
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.tag {
					display: flex;
					align-items: center;

					.tag-view {
						margin-left: 10rpx;
					}
				}

				.price-icon {
					display: flex;
					justify-content: space-between;

					.price {
						font-weight: bold;
						padding: 5rpx 0;
						font-size: 28rpx;
						color: #cf4444;
					}

					.icons {
						margin-left: auto;
						margin-right: 30rpx;
					}
				}


			}
		}
	}
</style>